package com.ruoyi.fyhs.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.fyhs.domain.Kt;
import com.ruoyi.fyhs.service.IKtService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 费用核算课题Controller
 *
 * @author huacai
 * @date 2024-04-16
 */
@RestController
@RequestMapping("/fyhs/kt")
public class KtController extends BaseController
{
    @Autowired
    private IKtService ktService;

    /**
     * 查询费用核算课题列表
     */
    @PreAuthorize("@ss.hasPermi('fyhs:kt:list')")
    @GetMapping("/list")
    public TableDataInfo list(Kt kt)
    {
        startPage();
        List<Kt> list = ktService.selectKtList(kt);
        return getDataTable(list);
    }

    /**
     * 导出费用核算课题列表
     */
    @PreAuthorize("@ss.hasPermi('fyhs:kt:export')")
    @Log(title = "费用核算课题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Kt kt)
    {
        List<Kt> list = ktService.selectKtList(kt);
        ExcelUtil<Kt> util = new ExcelUtil<Kt>(Kt.class);
        util.exportExcel(response, list, "费用核算课题数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Kt> util = new ExcelUtil<Kt>(Kt.class);
        util.importTemplateExcel(response, "费用核算课题数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "费用核算课题", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('fyhs:kt:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Kt> util = new ExcelUtil<Kt>(Kt.class);
        InputStream inputStream = file.getInputStream();
        List<Kt> list = util.importExcel(inputStream );
        inputStream.close();
        int count = ktService.batchInsertKt(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取费用核算课题详细信息
     */
    @PreAuthorize("@ss.hasPermi('fyhs:kt:query')")
    @GetMapping(value = "/{ktId}")
    public AjaxResult getInfo(@PathVariable("ktId") Integer ktId)
    {
        return success(ktService.selectKtByKtId(ktId));
    }

    /**
     * 新增费用核算课题
     */
    @PreAuthorize("@ss.hasPermi('fyhs:kt:add')")
    @Log(title = "费用核算课题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Kt kt)
    {
        return toAjax(ktService.insertKt(kt));
    }

    /**
     * 修改费用核算课题
     */
    @PreAuthorize("@ss.hasPermi('fyhs:kt:edit')")
    @Log(title = "费用核算课题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Kt kt)
    {
        return toAjax(ktService.updateKt(kt));
    }

    /**
     * 删除费用核算课题
     */
    @PreAuthorize("@ss.hasPermi('fyhs:kt:remove')")
    @Log(title = "费用核算课题", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ktIds}")
    public AjaxResult remove(@PathVariable Integer[] ktIds)
    {
        return toAjax(ktService.deleteKtByKtIds(ktIds));
    }
}
